package com.shr.code_interview;

import java.util.Stack;

public class Solution_52 {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
    	
    	ListNode commomNode = null;
 		Stack<ListNode> head1Stack = new Stack<>();
 		Stack<ListNode> head2Stack = new Stack<>();
 		ListNode p1 = pHead1 ;
 		while(p1 != null){
 			head1Stack.push(p1);
 			p1 = p1.next ;
 		}
 		ListNode p2 = pHead2;
 		while(p2 != null){
 			head2Stack.push(p2) ;
 			p2 = p2.next;
 		}
 		
 		while(!head1Stack.isEmpty() && !head2Stack.isEmpty() && head1Stack.peek() == head2Stack.peek()){
 			commomNode = head1Stack.pop();
 			head2Stack.pop();
 		}
 		return commomNode ;
    }	
}
